//
//  AddressBook.m
//  OC_JiangMingjun
//
//  Created by apple on 16/4/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AddressBook.h"
#import "Person.h"
#import "NSString+Characters.h"


@interface AddressBook ()

@property(nonatomic,retain)NSMutableDictionary *allPersonDic;

@end


@implementation AddressBook

#pragma mark - Creating and Init

- (instancetype)init{

    self = [super init];
    
    if (self) {
        
        self.allPersonDic = [NSMutableDictionary dictionary];
        
    }
    return self;
    
}


#pragma mark - Dealloc

- (void)dealloc{

    [_allPersonDic release];

    [super dealloc];

}


#pragma mark - Insert

- (BOOL)addPerson:(Person *)aperson{


    if (aperson.name == nil  || aperson.phoneNumber == nil ) {
        
        NSLog(@"添加失败");
        return NO;
        
    }else{
    
        NSMutableArray *arr = [self.allPersonDic objectForKey:aperson.group];
    
        if (arr == nil) {
            
            NSMutableArray *new = [NSMutableArray array];
            
            [new addObject:aperson];
            
            [self.allPersonDic setObject:new forKey:aperson.group];
            
        }else{
            
            if ([self findPersonWithName:aperson.name] == nil) {
                
                [arr addObject:aperson];
                
            }else{
            
                return NO;
            }
        }
        
        
        return YES;
    
    }

}



#pragma mark - Select

-(Person *)findPersonWithPhoneNum:(NSString *)aphoneNum{

    NSArray *arr = [self.allPersonDic allValues];
    
    for (NSArray<Person *> *parr in arr) {
        
        for (Person *p in parr) {
            
            if ([p.phoneNumber isEqualToString:aphoneNum]) {
                return p;
            }
            
        }

        
    }
    
    
    return nil;

}

- (Person *)findPersonWithName:(NSString *)name{

    
    NSString *group = [NSString getFirstChar:name];
    
    NSArray *arr = [self.allPersonDic objectForKey:group];
    
    for (Person *p in arr) {
        
        if ([name isEqualToString:p.name]) {
            return p;
        }
        
    }
    
    return nil;

}


- (void)showAll{

    NSArray *arr = [self.allPersonDic allValues];
    
    for (NSArray<Person *>*parr in arr) {
        
        NSLog(@"%@",parr);
   
    }
    
    

}


- (NSArray *)findPersonsWithSex:(NSString *)aSex{

    NSArray *arr = [self.allPersonDic allValues];
    
    NSMutableArray *newarr = [NSMutableArray array];
    
    for (NSArray<Person *> *parr in arr) {
        
        for (Person *p in parr) {
            
            if ([aSex isEqualToString:p.sex]) {
                
                [newarr addObject:p];
                
            }
            
        }
    }
    
    
    if (newarr != nil) {
       
        [newarr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            
            Person *p1 = (Person *)obj1;
            Person *p2 = (Person *)obj2;
            
            if (p1.age > p2.age) {
                return NSOrderedDescending;
            }else if (p1.age < p2.age){
            
                return NSOrderedAscending;
            }else{
            
                return NSOrderedSame;
            }
            
        }];
  
    }
    
    return newarr;

}


#pragma mark - Update

- (BOOL)changePersonWithName:(NSString *)name
                    phoneNum:(NSString *)aNum
                         sex:(NSString *)aSex
                         age:(NSInteger)aAge{


    Person *per = [self findPersonWithName:name];

    if (per != nil) {
        if (aNum != nil && aSex != nil && aAge != 0 ) {
           
            per.phoneNumber = aNum;
            per.sex = aSex;
            per.age = aAge;
            
            return YES;
            
        }
        
        return  NO;
        
    }else{
    
        return NO;
    }


}


#pragma mark - Delete


- (BOOL)deleteGroup:(NSString *)aGroupName{


    NSArray *arr = [self.allPersonDic objectForKey:[NSString getFirstChar:aGroupName]];
    
    if (arr != nil) {
        
        [self.allPersonDic removeObjectForKey:[NSString getFirstChar:aGroupName]];
        
        return YES;
        
    }

    return NO;
    
}

- (BOOL)deletePersonWithName:(NSString *)aPersonName{

    Person * p = [self findPersonWithName:aPersonName];
    
    if (p != nil) {
        
        NSMutableArray * arr = [self.allPersonDic objectForKey:[NSString getFirstChar:aPersonName]];
        
        [arr removeObject:p];
        
        return YES;
        
    }
    
    return  NO;

}







@end
